package com.xkcyy.one.controller;

import com.xkcyy.one.common.Result;
import com.xkcyy.one.entity.Agent;
import com.xkcyy.one.service.AgentCollaborationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 智能体协作关联控制器
 * 
 * @author yuand
 */
@RestController
@RequestMapping("/api/agent-collaborations")
public class AgentCollaborationController {

    private final AgentCollaborationService collaborationService;

    @Autowired
    public AgentCollaborationController(AgentCollaborationService collaborationService) {
        this.collaborationService = collaborationService;
    }

    /**
     * 获取主智能体可协作的从智能体列表
     *
     * @param agentId 主智能体ID
     * @return 从智能体列表
     */
    @GetMapping("/agent/{agentId}")
    public Result<List<Agent>> getSubAgentsByAgentId(@PathVariable Long agentId) {
        return Result.success(collaborationService.getSubAgentsByAgentId(agentId));
    }

    /**
     * 获取可以使用从智能体的主智能体列表
     *
     * @param subAgentId 从智能体ID
     * @return 主智能体列表
     */
    @GetMapping("/sub-agent/{subAgentId}")
    public Result<List<Agent>> getAgentsBySubAgentId(@PathVariable Long subAgentId) {
        return Result.success(collaborationService.getAgentsBySubAgentId(subAgentId));
    }

    /**
     * 添加智能体协作关系
     *
     * @param agentId    主智能体ID
     * @param subAgentId 从智能体ID
     * @return 操作结果
     */
    @PostMapping("/add")
    public Result<Void> addCollaboration(@RequestParam Long agentId, @RequestParam Long subAgentId) {
        collaborationService.addCollaboration(agentId, subAgentId);
        return Result.success();
    }

    /**
     * 批量添加协作关系
     *
     * @param agentId     主智能体ID
     * @param subAgentIds 从智能体ID列表
     * @return 添加的从智能体ID列表
     */
    @PostMapping("/add-batch")
    public Result<List<Long>> addCollaborations(@RequestParam Long agentId, @RequestBody List<Long> subAgentIds) {
        return Result.success(collaborationService.addCollaborations(agentId, subAgentIds));
    }

    /**
     * 移除智能体协作关系
     *
     * @param agentId    主智能体ID
     * @param subAgentId 从智能体ID
     * @return 操作结果
     */
    @DeleteMapping("/remove")
    public Result<Void> removeCollaboration(@RequestParam Long agentId, @RequestParam Long subAgentId) {
        collaborationService.removeCollaboration(agentId, subAgentId);
        return Result.success();
    }

    /**
     * 批量移除协作关系
     *
     * @param agentId     主智能体ID
     * @param subAgentIds 从智能体ID列表
     * @return 移除的从智能体ID列表
     */
    @DeleteMapping("/remove-batch")
    public Result<List<Long>> removeCollaborations(@RequestParam Long agentId, @RequestBody List<Long> subAgentIds) {
        return Result.success(collaborationService.removeCollaborations(agentId, subAgentIds));
    }

    /**
     * 清空主智能体的所有协作关系
     *
     * @param agentId 主智能体ID
     * @return 操作结果
     */
    @DeleteMapping("/clear/{agentId}")
    public Result<Void> clearAgentCollaborations(@PathVariable Long agentId) {
        collaborationService.clearAgentCollaborations(agentId);
        return Result.success();
    }
}